import { Application } from "egg";
import CodoBase from "../../CodoBase";
import { Weaver } from "./ecology9";

export class WeaverEcologyHRMService extends CodoBase {
	constructor(public readonly baseUrl: string, app: Application) {
		super(app);
	}
	/** 获取人员信息列表
	 * @baseUrl ecology9的内网地址 eg: http://192.168.1.1:8089 weaver处配置好ip白名单即可访问，不需token
	 */
	async getHrmUserInfoWithPage(params: Weaver.Ecology9.HRM.IRequestGetUserInfoParams) {
		const { baseUrl } = this;
		const url = `${baseUrl}/api/hrm/resful/getHrmUserInfoWithPage`;

		const res = await this.curl<Weaver.Ecology9.HRM.IResponse<Weaver.Ecology9.HRM.IUserInfo>>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: {
				params,
			},
		});
		// if (res.data.code !== "1") throw new Error(` Got Error.`);
		return res.data;
	}

	/** 获取公司信息列表
	 * @baseUrl ecology9的内网地址 eg: http://192.168.1.1:8089
	 */
	async getHrmsubcompanyWithPage(params: Weaver.Ecology9.HRM.IRequestGetSubCompanyParams) {
		const { baseUrl } = this;

		// /api/hrm/resful/getHrmsubcompanyWithPage
		const url = `${baseUrl}/api/hrm/resful/getHrmsubcompanyWithPage`;

		const res = await this.curl<Weaver.Ecology9.HRM.IResponse<Weaver.Ecology9.HRM.ISubCompany>>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: {
				params,
			},
		});

		// if (res.data.code !== "1") throw new Error(` Got Error.`);
		return res.data;
	}

	/** 获取部门列表
	 * @baseUrl ecology9的内网地址 eg: http://192.168.1.1:8089
	 *
	 */
	async getHrmdepartmentWithPage(params: Weaver.Ecology9.HRM.IRequestGetDepartmentParams) {
		const { baseUrl } = this;

		// /api/hrm/resful/getHrmdepartmentWithPage
		const url = `${baseUrl}/api/hrm/resful/getHrmdepartmentWithPage`;
		const res = await this.curl<Weaver.Ecology9.HRM.IResponse<Weaver.Ecology9.HRM.IDepartment>>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: {
				params,
			},
		});
		// if (res.data.code !== "1") throw new Error(` Got Error.`);
		return res.data;
	}

	/** 获取岗位列表
	 * @baseUrl ecology9的内网地址 eg: http://192.168.1.1:8089
	 *
	 */
	async getJobtitleInfoWithPage(params: Weaver.Ecology9.HRM.IRequestGetJobTitleParams) {
		const { baseUrl } = this;

		const url = `${baseUrl}/api/hrm/resful/getJobtitleInfoWithPage`;
		const res = await this.curl<Weaver.Ecology9.HRM.IResponse<Weaver.Ecology9.HRM.IJobTitle>>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: {
				params,
			},
		});
		// if (res.data.code !== "1") throw new Error(` Got Error.`);
		return res.data;
	}

	/**
	 * 同步人员信息
	 * @baseUrl ecology9的内网地址 eg: http://192.168.1.1:8089
	 */
	async synHrmresource(data: Weaver.Ecology9.HRM.IRequestSynHrmResource[]) {
		const { baseUrl } = this;

		// /api/hrm/resful/synHrmresource
		const url = `${baseUrl}/api/hrm/resful/synHrmresource`;

		type ISyncResult = {
			result?: {
				[workCode: string]: {
					msg: string;
					code: string;
				};
			};
			msg?: string;

			code: string;
		};
		const res = await this.curl<ISyncResult>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: { data },
		});
		// if (res.data.code !== "1") throw new Error(` Got Error.`);
		return res.data;
	}

	/**
	 * 同步部门信息
	 * @baseUrl ecology9的内网地址 eg: http://192.168.1.1:8089
	 * */
	async synDepartment(data: Weaver.Ecology9.HRM.IRequestSynDepartment[]) {
		const { baseUrl } = this;

		const url = `${baseUrl}/api/hrm/resful/synDepartment`;
		type ISyncResult = {
			result?: {
				[key: string]: {
					msg: string;
					code: string;
				};
			};
			msg?: string;
			code: string;
		};

		const res = await this.curl<ISyncResult>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: { data },
		});
		// if (res.data.code !== "1") throw new Error(` Got Error.`);
		return res.data;
	}

	/**
	 * 同步岗位信息
	 * @baseUrl ecology9的内网地址 eg: http://192.168.1.1:8089
	 * */
	async synJobtitle(data: Weaver.Ecology9.HRM.IRequestSynJobtitle[]) {
		const { baseUrl } = this;

		const url = `${baseUrl}/api/hrm/resful/synJobtitle`;
		type ISyncResult = {
			result?: {
				[key: string]: {
					msg: string;
					code: string;
				};
			};
			msg?: string;
			code: string;
		};

		const res = await this.curl<ISyncResult>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: { data },
		});
		// if (res.data.code !== "1") throw new Error(` Got Error.`);
		return res.data;
	}

	/**
	 * 同步分部信息
	 * @baseUrl ecology9的内网地址 eg: http://192.168.1.1:8089
	 * */
	async synSubcompany(data: Weaver.Ecology9.HRM.IRequestSynSubcompany[]) {
		const { baseUrl } = this;

		const url = `${baseUrl}/api/hrm/resful/synSubcompany`;
		type ISyncResult = {
			result?: {
				[key: string]: {
					msg: string;
					code: string;
				};
			};
			msg?: string;
			code: string;
		};

		const res = await this.curl<ISyncResult>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: { data },
		});
		// if (res.data.code !== "1") throw new Error(` Got Error.`);
		return res.data;
	}
}
